//
//  DKHomeViewModel.swift
//  Sina
//
//  Created by 赵柏清 on 16/8/1.
//  Copyright © 2016年 DarkZhao. All rights reserved.
//

import UIKit
import SDWebImage

class DKHomeViewModel: NSObject {
    /*
    https://api.weibo.com/2/statuses/home_timeline.json
    JSON格式
    GET请求
    必选参数access_Token
    */
    
    lazy var modelList : [DKHomeCellViewModel] = [DKHomeCellViewModel]()
    
    func getDates(since_id : Int,max_id : Int,success : ()->(),failed : ()->()) {
        
        let URLString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let parameters = ["access_token" : DKLoginViewModel.sharedInstance.access_Token,
                              "since_id" : since_id,
                                "max_id" : max_id]
        
        DKNetworkingTool.sharedInstance.GetHttpClient(URLString, parameters: parameters, progress: nil, success: { (_, JSON) -> Void in
            
            // MARK: 成功之后的数据处理
            guard let OJSON = JSON as? [String : AnyObject] else {
                return
            }
            
            // 创建一个临时数组
            var tempArray : [DKHomeCellViewModel] = [DKHomeCellViewModel]()
            
            // 创建一个调度组
            let group = dispatch_group_create()
            
            if let statusArray = OJSON["statuses"] as? [[String : AnyObject]] {
                
                // 遍历数组
                for status in statusArray {
                    
                    // 字典转模型
                    let statusModel = DKStatusModel(dict: status)
                    
                    // MARK: 判断单张图片并下载
                    let pic_urls = statusModel.pic_urls == nil ? (statusModel.retweeted_status?.pic_urls) : statusModel.pic_urls
                    
                    if let pic_URL = pic_urls where pic_urls?.count == 1 {
                        guard let urlStrng = pic_URL.first?.thumbnail_pic else {
                            return
                        }
                        
                        dispatch_group_enter(group)
                        let URL = NSURL(string: urlStrng)!
                        
                        SDWebImageManager.sharedManager().downloadImageWithURL(URL, options: [], progress: nil, completed: { (image, error, _, _, _) -> Void in
                            dispatch_group_leave(group)
                        })
                        
                    }
                    
                    let cellViewModel = DKHomeCellViewModel()
                    cellViewModel.statusModel = statusModel
                    
                    tempArray.append(cellViewModel)
                }
                
                if max_id > 0 {
                    self.modelList = self.modelList + tempArray
                } else {
                    self.modelList = tempArray + self.modelList
                }
                
                
            }
            
            dispatch_group_notify(group, dispatch_get_main_queue(), { () -> Void in
                // 当所有的单张都下载完成之后再返回调用
                success()
            })
            
            
            }) { (_, error) -> Void in
                failed()
        }
        
    }

}
